package cn.lblbc.webbrowser

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.lblbc.webbrowser.models.Bookmark
import cn.lblbc.webbrowser.utils.BookmarkManager

class BookmarksActivity : BaseActivity() {
    private lateinit var bookmarksRecyclerView: RecyclerView
    private lateinit var bookmarksAdapter: BookmarksAdapter
    private lateinit var closeButton: ImageButton
    private lateinit var emptyStateText: TextView
    private lateinit var bookmarkManager: BookmarkManager
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bookmarks)
        
        // 设置 Toolbar 为 ActionBar
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.title = "书签"
        
        initViews()
        setupRecyclerView()
        setupListeners()
    }
    
    override fun onSupportNavigateUp(): Boolean {
        finish() // 返回按钮点击事件
        return true
    }
    
    override fun onResume() {
        super.onResume()
        loadBookmarks()
    }
    
    private fun initViews() {
        bookmarksRecyclerView = findViewById(R.id.bookmarksRecyclerView)
        closeButton = findViewById(R.id.closeButton)
        emptyStateText = findViewById(R.id.emptyStateText)
        bookmarkManager = BookmarkManager(this)
    }
    
    private fun setupRecyclerView() {
        bookmarksRecyclerView.layoutManager = LinearLayoutManager(this)
        
        bookmarksAdapter = BookmarksAdapter(
            onBookmarkClicked = { bookmark ->
                // 这里需要实现跳转逻辑，例如返回主Activity并加载URL
                (this as? MainActivity)?.loadUrl(bookmark.url)
                finish() // 关闭当前Activity
            },
            onDeleteClicked = { bookmark ->
                bookmarkManager.removeBookmark(bookmark.url)
                loadBookmarks()
            }
        )
        
        bookmarksRecyclerView.adapter = bookmarksAdapter
    }
    
    private fun setupListeners() {
        closeButton.setOnClickListener {
            finish() // 关闭当前Activity
        }
    }
    
    private fun loadBookmarks() {
        val bookmarks = bookmarkManager.getBookmarks()
        bookmarksAdapter.submitList(bookmarks)
        
        if (bookmarks.isEmpty()) {
            bookmarksRecyclerView.visibility = View.GONE
            emptyStateText.visibility = View.VISIBLE
        } else {
            bookmarksRecyclerView.visibility = View.VISIBLE
            emptyStateText.visibility = View.GONE
        }
    }
    
    class BookmarksAdapter(
        private val onBookmarkClicked: (Bookmark) -> Unit,
        private val onDeleteClicked: (Bookmark) -> Unit
    ) : RecyclerView.Adapter<BookmarksAdapter.BookmarkViewHolder>() {
        
        private var bookmarks: List<Bookmark> = emptyList()
        
        fun submitList(newList: List<Bookmark>) {
            bookmarks = newList
            notifyDataSetChanged()
        }
        
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookmarkViewHolder {
            val view = android.view.LayoutInflater.from(parent.context)
                .inflate(R.layout.item_bookmark, parent, false)
            return BookmarkViewHolder(view)
        }
        
        override fun onBindViewHolder(holder: BookmarkViewHolder, position: Int) {
            val bookmark = bookmarks[position]
            
            holder.bookmarkTitle.text = bookmark.title
            holder.bookmarkUrl.text = bookmark.url
            
            holder.itemView.setOnClickListener { onBookmarkClicked(bookmark) }
            holder.deleteButton.setOnClickListener { onDeleteClicked(bookmark) }
        }
        
        override fun getItemCount(): Int = bookmarks.size
        
        class BookmarkViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val bookmarkIcon: ImageView = itemView.findViewById(R.id.bookmarkIcon)
            val bookmarkTitle: TextView = itemView.findViewById(R.id.bookmarkTitle)
            val bookmarkUrl: TextView = itemView.findViewById(R.id.bookmarkUrl)
            val deleteButton: ImageButton = itemView.findViewById(R.id.deleteButton)
        }
    }
}